package com.zhangyongyi.carhome.fragment.recommend.persuader;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.zhangyongyi.carhome.R;
import com.zhangyongyi.carhome.activity.WebViewActivity;
import com.zhangyongyi.carhome.adapter.recommend.persuader.persuaderRecyclerViewAdapter;
import com.zhangyongyi.carhome.db.DaoHelper;
import com.zhangyongyi.carhome.entity.greendao.PersuaderList;
import com.zhangyongyi.carhome.entity.recommend.Persuader.PersuaderEntity;
import com.zhangyongyi.carhome.entity.recommend.Persuader.Result;
import com.zhangyongyi.carhome.tools.DividerItemDecoration;
import com.zhangyongyi.carhome.tools.MyOnClickListener;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * Created by dllo on 16/1/19.
 * 说课
 */
public class PersuaderFragment extends Fragment implements MyOnClickListener {
    private RecyclerView recyclerView;
    private persuaderRecyclerViewAdapter adapter;
    private RequestQueue queue;
    private DaoHelper helper;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_recommend_persuader, null);
        return v;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        recyclerView = (RecyclerView) getActivity().findViewById(R.id.rv_persuader);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 1);
        recyclerView.setLayoutManager(gridLayoutManager);

        adapter = new persuaderRecyclerViewAdapter(getActivity());
        //分割线
        recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
        recyclerView.setAdapter(adapter);

        adapter.setMyOnClickListener(this);
        initData();

        helper = DaoHelper.getInstance();

    }

    private void initData() {
        queue = Volley.newRequestQueue(getActivity());
        String url = "http://app.api.autohome.com.cn/autov4.8.5/news/shuokelist-pm2-s20-lastid0.json";
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                PersuaderEntity pl = gson.fromJson(response, PersuaderEntity.class);
                if (pl != null) {
                    Result r = pl.getResult();
                    if (r != null) {
                        List<PersuaderList> persuaderLists = r.getList();
                        if (persuaderLists != null && persuaderLists.size() > 0) {
                            adapter.addData(persuaderLists);
                            Log.d("xx", "persuaderLists");
                            helper.addPersuaderList(persuaderLists);
                        }
                    }
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
                adapter.addData(helper.seacherPersuaderList());

            }
        }) {
            /**防止乱码 **/
            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                try {
                    String jsonString = new String(response.data, "UTF-8");
                    return Response.success(jsonString,
                            HttpHeaderParser.parseCacheHeaders(response));
                } catch (UnsupportedEncodingException e) {
                    return Response.error(new ParseError(e));
                } catch (Exception je) {
                    return Response.error(new ParseError(je));
                }
            }
        };
        queue.add(request);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (queue != null) {
            queue.cancelAll(this);
        }
    }

    @Override
    public void MyOnClickListener(int id) {
        Intent intent = new Intent();
        intent.setClass(getActivity(), WebViewActivity.class);
        intent.putExtra("id",id);
        getActivity().startActivity(intent);
    }
}
